package com.zmn.oms.api.controller.third.baidu_dtw;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.baidu_dtw.BaiduDtwConsts;
import com.zmn.oms.third.baidu_dtw.service.BaiduDtwService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 类描述：
 *
 * @author xiewenbing
 * @date 2020/11/24 11:50
 */
@RestController
@RequestMapping("/baidu/api")
@Slf4j
public class BaiduApiController {

    @Autowired
    private BaiduDtwService baiduDtwService;

    @RequestMapping("/newOrder")
    @ResponseBody
    public ResultDTO pullOrder(HttpServletRequest request) {
        String paramsStr = RequestUtil.getPostParameter(request);
        String paramId = request.getParameter("channelId");
        if (StringUtil.isBlank(paramId)) {
            return ResultDTO.fail("缺少参数channelId");
        }

        log.info("【{}】收到新单消息！ ChannelId:[{}],Body:[{}]",  BaiduDtwConsts.NAME, paramId,paramsStr);

        try {
            Integer channelId = Integer.valueOf(paramId);
            if (NumberUtil.isNullOrZero(channelId)) {
                return ResultDTO.fail("ChannelId不能为0或空");
            }

            baiduDtwService.procMsg(paramsStr, channelId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultDTO.fail("失败");
        }

        return ResultDTO.success();
    }
}
